﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.Extensions;
using eDirectory.Common.Dto.Address;
using eDirectory.Common.Dto.Customer;
using eDirectory.Common.Message;
using eDirectory.Domain.AppServices;
using eDirectory.Domain.Services;
using eDirectory.Domain.TransManager;
using eDirectory.Mvc.Models;
using eDirectory.Mvc.Services;

namespace eDirectory.Mvc.Controllers
{
    public class CustomerController : eDirectoryControllerBase
    {
        //
        // GET: /Customer/

        [ResponseManager]
        public ActionResult Index()
        {
            Container.RequestContext.Notifier.AddWarning(BusinessWarningEnum.Default, "Log :: Customer.Index method was invoked");
            return View(new CustomerModel());
        }

        [GridAction]
        [ResponseManager]
        public ActionResult SelectAjax()
        {
            return View(new GridModel(GetAllCustomers().CustomerList));
        }

        [GridAction]
        [ResponseManager]
        public ActionResult InsertAjax()
        {
            var dto = new CustomerDto();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(dto))
            {
                var service = new CustomerService();
                CheckDtoResponse(service.CreateNewCustomer(dto)); 
            }
            return View(new GridModel(GetAllCustomers().CustomerList));
        }

        [GridAction]
        [ResponseManager]
        public ActionResult DeleteAjax(long id)
        {
            var service = new CustomerService();
            CheckDtoResponse(service.DeleteCustomer(id)); 
            var dto = new CustomerDto();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(dto))
            {
                
                CheckDtoResponse(service.CreateNewCustomer(dto));
            }
            return View(new GridModel(GetAllCustomers().CustomerList));
        }


        [GridAction]
        [ResponseManager]
        public ActionResult SaveAjax(long id)
        {
            //Create a new instance of the EditableProduct class.
            var dto = new CustomerDto();
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(dto))
            {
                //The model is valid - insert the product and redisplay the grid.
                var service = new CustomerService();
                CheckDtoResponse(service.UpdateCustomer(dto));
            }
            return View(new GridModel(GetAllCustomers().CustomerList));
        }

        [GridAction]
        [ResponseManager]
        public ActionResult SelectAddressesAjax(long customerId)
        {
            var service = new CustomerService();
            var customer = service.GetById(customerId);
            CheckDtoResponse(customer);
            return View(new GridModel(customer.Addresses));
        }

        [GridAction]
        [ResponseManager]
        public ActionResult InsertAddressAjax(long customerId)
        {
            var dto = new AddressDto();
            if (TryUpdateModel(dto))
            {
                var service = new AddressService();
                dto.CustomerId = customerId;
                CheckDtoResponse(service.CreateNewAddress(dto));
            }
            return SelectAddressesAjax(customerId);
        }

        private CustomerModel GetAllCustomers()
        {
            var service = new CustomerService();
            var allCustomers = service.FindAll();
            CheckDtoResponse(allCustomers);
            return new CustomerModel { CustomerList = allCustomers.Customers };
        }
    }

}
